package cn.enjoy.redisson.controller;

import cn.enjoy.redisson.entity.SomeObject;
import lombok.AllArgsConstructor;
import org.redisson.api.RBitSet;
import org.redisson.api.RBucket;
import org.redisson.api.RedissonClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

/**
 * 分布式对象
 *
 * @author K
 * @date 2024-04-24 15:44
 */
@RestController
@AllArgsConstructor
@RequestMapping("object")
public class ObjectController {

    private final RedissonClient redisson;

    @GetMapping("bitSet")
    public void bitSet() {
        RBitSet bitSet = redisson.getBitSet("bitSet");

        // 50亿url中判断有没有重复url
//        for (long i = 0; i < 5000000000L; i++) {
        for (long i = 0; i < 10L; i++) {
            String url = "https://www.a" + i+".com";
            bitSet.set(url.hashCode(), true);
        }

        boolean b = bitSet.get("https://www.a1.com".hashCode());
        long size = bitSet.size();
        System.out.println();
    }

    @GetMapping("simpleObject")
    public void simpleObject() {
        SomeObject someObject = new SomeObject("张三", 18);
        RBucket<SomeObject> bucket = redisson.getBucket("simpleObject");
        bucket.set(someObject, 1, TimeUnit.HOURS);

        SomeObject queryObject = bucket.get();

        System.out.println();
    }

}
